package com.mobile.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.mobile.pojo.OderDetailVO;
import com.mobile.pojo.ReservationDTO;
import com.mobile.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Encounter
 * @date 2024/10/12 11:45<br/>
 * 客户端预约操作
 */
@RestController("clientOrderController")
@Slf4j
@RequestMapping("/order")
public class OrderController
    {
        @Autowired
        private OrderService orderService;
        
        /**
         * 找到由id
         *
         * @param id id
         * @return {@link Result }
         */
        @GetMapping("/findById")
        public Result findById(@RequestParam Integer id)
            {
                try
                    {
                        log.info("根据id查询：{}", id);
                        OderDetailVO oderDetailVO = orderService.findById(id);
                        if (oderDetailVO != null)
                            {
                                return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, oderDetailVO);
                            }
                        else
                            {
                                return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
                            }
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
                    }
            }
        
        /**
         * 提交订单
         *
         * @param reservationDTO 预留 DTO
         * @return {@link Result }
         */
        @PostMapping("submitOrder")
        public Result submitOrder(@RequestBody ReservationDTO reservationDTO)
            {
                try
                    {
                        log.info("体检预约：{}", reservationDTO);
                        int num = orderService.reservation(reservationDTO);
                        if (num >= 0)
                            {
                                return new Result(true, MessageConstant.ORDER_SUCCESS, num);
                            }
                        else
                            {
                                return new Result(false, MessageConstant.ORDER_FULL);
                            }
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return ResponseEntity.badRequest().body(new Result(false, e.getMessage())).getBody();
                    }
            }
    }
